package sh.wnxy.wn38.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import sh.wnxy.wn38.entity.Ticket;
import sh.wnxy.wn38.entity.User;
import sh.wnxy.wn38.mapper.TicketMapper;
import sh.wnxy.wn38.service.ITicketService;
import sh.wnxy.wn38.service.IUserService;
import sh.wnxy.wn38.service.vo.TicketQuery;
import sh.wnxy.wn38.utils.Result;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wn38
 * @since 2021-07-14
 */
@RestController
@CrossOrigin
@RequestMapping("/ticket")
public class TicketController {
    private ITicketService iTicketService;

    @Autowired
    public void setITicketService(ITicketService iTicketService) {
        this.iTicketService = iTicketService;
    }



    @ApiOperation(value = "查询票种")
    @PostMapping("findTicket")
    public Result findTicket(@RequestBody TicketQuery ticketQuery) {

        QueryWrapper<Ticket> ticketQueryWrapper = new QueryWrapper<>();
        Integer spotId = ticketQuery.getTicketSpotid();
        String kind = ticketQuery.getTicketVersion();
        Date date = ticketQuery.getTicketDate();
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();


        if (!StringUtils.isEmpty(kind)) {
            ticketQueryWrapper.eq("ticket_version", kind);
        }

        ticketQueryWrapper.eq("ticket_spotid", spotId);

        ticketQueryWrapper.eq("ticket_date", localDate);

        Ticket one = iTicketService.getOne(ticketQueryWrapper);
        return one != null ? Result.ok().data("Ticket", one) : Result.error();
    }



    @ApiOperation(value = "新增票种")
    @PostMapping("addTicket")
    public Result addTicket(@RequestBody TicketQuery ticketQuery) {
        Date ticketDate = ticketQuery.getTicketDate();
        LocalDate localDate = ticketDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        Integer ticketSpotid = ticketQuery.getTicketSpotid();
        String ticketVersion = ticketQuery.getTicketVersion();
        Ticket ticket = new Ticket(0, 100, ticketSpotid, 100.0, ticketVersion, localDate);
        boolean save = iTicketService.save(ticket);
        return save? Result.ok().data("newTicket",ticket) : Result.error();
    }

}


